﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using UtilZ.Dotnet.Ex.Attributes;
using UtilZ.Dotnet.Ex.ZLog;

namespace UtilZ.Dotnet.Ex.Model
{
    /// <summary>
    /// 对象构建器类
    /// </summary>
    /// <typeparam name="TId">数据类型值类型</typeparam>
    /// <typeparam name="TObjectType">创建实例所需要转换的基类或接口类型</typeparam>
    public class ObjectBuilder<TId, TObjectType> : IReadOnlyDictionary<TId, ObjectBuilderTypeInfo>
    {
        /// <summary>
        /// 类型字典集合 [key:数据类型;value:类型]
        /// </summary>
        private readonly Dictionary<TId, ObjectBuilderTypeInfo> _typeDic = new Dictionary<TId, ObjectBuilderTypeInfo>();

        /// <summary>
        /// 类型必须继承的基类,为null表示不验证
        /// </summary>
        private readonly Type _baseClassType = null;

        /// <summary>
        /// 类型必须实现的接口,为null表示不验证
        /// </summary>
        private readonly ReadOnlyCollection<Type> _interfaceTypes = null;

        /// <summary>
        /// 必须的构造函数参数类型集合,为null或空集合,表示仅使用无参构造函数
        /// </summary>
        private readonly Type[] _requiredConstructorParameterTypes;


        /// <summary>
        /// 错误当TDataType类型为数值类型时,消息中值格式[true:十六进制格式;false:十进制格式,默认为16进制]
        /// </summary>
        private bool _errorMessageValueFormat = true;
        /// <summary>
        /// 错误当TDataType类型为数值类型时,消息中值格式[true:十六进制格式;false:十进制格式,默认为16进制]
        /// </summary>
        public bool ErrorMessageValueFormat
        {
            get { return _errorMessageValueFormat; }
            set { _errorMessageValueFormat = value; }
        }


        private readonly IObjectBuildFactory<TId, TObjectType> _objectBuildFactory;
        /// <summary>
        /// 对象构建工厂
        /// </summary>
        public IObjectBuildFactory<TId, TObjectType> ObjectBuildFactory
        {
            get { return this._objectBuildFactory; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public ObjectBuilder()
        {
            Type objInsType = typeof(TObjectType);
            List<Type> interfaceTypeList = null;
            if (objInsType.IsClass)
            {
                this._baseClassType = objInsType;
            }
            else if (objInsType.IsInterface)
            {
                interfaceTypeList = new List<Type>() { objInsType };
            }
            else
            {
                //结构体
            }

            if (interfaceTypeList != null)
            {
                this._interfaceTypes = new ReadOnlyCollection<Type>(interfaceTypeList);
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="objectBuildFactory">对象构建工厂</param>
        /// <param name="requiredConstructorParameterTypes">必须的构造函数参数类型集合,为null或空集合,表示仅使用无参构造函数</param>
        public ObjectBuilder(IObjectBuildFactory<TId, TObjectType> objectBuildFactory, Type[] requiredConstructorParameterTypes)
            : this()
        {
            this._objectBuildFactory = objectBuildFactory;
            this._requiredConstructorParameterTypes = requiredConstructorParameterTypes;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="baseClassType">通过类型必须继承某个基类过滤掉不符合要求的类型,为null表示不验证</param>
        /// <param name="interfaceTypes">通过必须实现现的接口过滤掉不符合要求的类型,为null表示不验证</param>
        /// <param name="objectBuildFactory">对象构建工厂</param>
        /// <param name="requiredConstructorParameterTypes">必须的构造函数参数类型集合,为null或空集合,表示仅使用无参构造函数</param>
        public ObjectBuilder(Type baseClassType, IEnumerable<Type> interfaceTypes, IObjectBuildFactory<TId, TObjectType> objectBuildFactory = null, Type[] requiredConstructorParameterTypes = null)
        {
            if (baseClassType != null && !baseClassType.IsClass)
            {
                throw new ArgumentException($"基类类型只能为类类型,类型\"{baseClassType.FullName}\"无效", nameof(baseClassType));
            }

            Type objInsType = typeof(TObjectType);
            List<Type> interfaceTypeList = null;
            if (interfaceTypes != null)
            {
                interfaceTypeList = new List<Type>(interfaceTypes);
            }

            if (objInsType.IsClass)
            {
                if (baseClassType != null &&
                    baseClassType != objInsType &&
                    !objInsType.IsSubclassOf(baseClassType))
                {
                    throw new ArgumentException($"实例返回类型需要从类型\"{baseClassType.FullName}\"派生");
                }

                this._baseClassType = baseClassType;
            }
            else if (objInsType.IsInterface)
            {
                this._baseClassType = baseClassType;
                if (interfaceTypeList == null)
                {
                    interfaceTypeList = new List<Type>() { objInsType };
                }
                else
                {
                    if (!interfaceTypeList.Contains(objInsType))
                    {
                        interfaceTypeList.Add(objInsType);
                    }
                }
            }
            else
            {
                //结构体
            }

            if (interfaceTypeList != null)
            {
                this._interfaceTypes = new ReadOnlyCollection<Type>(interfaceTypeList);
            }

            this._objectBuildFactory = objectBuildFactory;
            this._requiredConstructorParameterTypes = requiredConstructorParameterTypes;
        }


        /// <summary>
        /// 设置指定的类型,如果存在则替换,不存在则添加
        /// </summary>
        /// <param name="typeId">数据类型值类型</param>
        /// <param name="type">要注册的类型</param>
        /// <exception cref="ArgumentException">参数不匹配异常</exception>
        public void Set(TId typeId, Type type)
        {
            if (typeId == null)
            {
                throw new ArgumentNullException(nameof(typeId));
            }

            if (this._typeDic.ContainsKey(typeId))
            {
                throw new ArgumentException($"数据类型值类型\"{typeId}\"已存在.");
            }

            string error;
            if (!this.CheckType(type, null, out error))
            {
                throw new ArgumentException(error);
            }

            ObjectBuilderTypeConstructorType? objectBuilderType = GetTypeConstructorType(type);
            if (objectBuilderType == null)
            {
                //无匹配的参构造函数,该类型忽略
                throw new ArgumentException($"类型\"{type.FullName}\"无匹配的参构造函数,该类型忽略...");
            }

            this._typeDic[typeId] = new ObjectBuilderTypeInfo(objectBuilderType.Value, type);
        }

        /// <summary>
        /// 添加指定类型
        /// </summary>
        /// <param name="typeId">数据类型值类型</param>
        /// <param name="type">要注册的类型</param>
        /// <exception cref="ArgumentException">参数不匹配异常</exception>
        public void RegisteType(TId typeId, Type type)
        {
            if (typeId == null)
            {
                throw new ArgumentNullException(nameof(typeId));
            }

            if (this._typeDic.ContainsKey(typeId))
            {
                throw new ArgumentException($"数据类型值类型\"{typeId}\"已存在.");
            }

            string error;
            if (!this.CheckType(type, null, out error))
            {
                throw new ArgumentException(error);
            }

            ObjectBuilderTypeConstructorType? objectBuilderType = GetTypeConstructorType(type);
            if (objectBuilderType == null)
            {
                //无匹配的参构造函数,该类型忽略
                throw new ArgumentException($"类型\"{type.FullName}\"无匹配的参构造函数,该类型忽略...");
            }

            this._typeDic.Add(typeId, new ObjectBuilderTypeInfo(objectBuilderType.Value, type));
        }

        /// <summary>
        /// 注册程序集中的类型
        /// </summary>
        /// <param name="assembly">注册程序集中的符合要求的类型</param>
        /// <param name="ignoreTypes">忽略的类型集合</param>
        /// <exception cref="ArgumentNullException"></exception>
        public void RegisteType(Assembly assembly, IEnumerable<Type> ignoreTypes = null)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException(nameof(assembly));
            }

            Type[] types = assembly.GetExportedTypes();
            PrimitiveRegisteType(types, false, ignoreTypes);
        }

        /// <summary>
        /// 注册类型集合
        /// </summary>
        /// <param name="types">类型集合</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public void RegisteType(IEnumerable<Type> types)
        {
            if (types == null)
            {
                throw new ArgumentNullException(nameof(types));
            }

            PrimitiveRegisteType(types, true, null);
        }

        /// <summary>
        /// 注册单个类型
        /// </summary>
        /// <param name="type">指定的目标类型</param>
        /// <exception cref="ArgumentNullException"></exception>
        public void RegisteType(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            PrimitiveRegisteType(new Type[] { type }, true, null);
        }

        /// <summary>
        /// 注册单个类型
        /// </summary>
        public void RegisteType<T>()
        {
            PrimitiveRegisteType(new Type[] { typeof(T) }, true, null);
        }

        /// <summary>
        /// 注册类型集合
        /// </summary>
        /// <param name="types"></param>
        /// <param name="validateType">是否验证类型</param>
        /// <param name="ignoreTypes">忽略的类型集合</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        private void PrimitiveRegisteType(IEnumerable<Type> types, bool validateType, IEnumerable<Type> ignoreTypes)
        {
            if (types == null)
            {
                throw new ArgumentNullException(nameof(types));
            }

            Type attType = typeof(ObjectBuildIdAttribute);
            object[] attributeArr;
            ObjectBuildIdAttribute typeIdAtt;
            Type typeIdType = typeof(TId);
            TId typeId;
            ObjectBuilderTypeConstructorType? objectBuilderType;
            ObjectBuilderTypeInfo oldBuilderTypeInfo;
            string error;

            foreach (var type in types)
            {
                if (!this.CheckType(type, ignoreTypes, out error))
                {
                    if (validateType)
                    {
                        throw new ArgumentException(error);
                    }
                    else
                    {
                        continue;
                    }
                }

                attributeArr = type.GetCustomAttributes(attType, true);
                if (attributeArr == null || attributeArr.Length == 0)
                {
                    if (validateType)
                    {
                        throw new ArgumentException($"类型\"{type.FullName}\"未标记特性\"{nameof(ObjectBuildIdAttribute)}\"");
                    }
                    else
                    {
                        continue;
                    }
                }

                objectBuilderType = this.GetTypeConstructorType(type);
                if (objectBuilderType == null)
                {
                    //无匹配的参构造函数,该类型忽略
                    ZLoger.Warn($"类型\"{type.FullName}\"无匹配的参构造函数,该类型忽略...");
                    continue;
                }

                foreach (var att in attributeArr)
                {
                    typeIdAtt = (ObjectBuildIdAttribute)att;
                    if (typeIdAtt.Id == null || typeIdAtt.Id.GetType() != typeIdType)
                    {
                        continue;
                    }

                    typeId = (TId)typeIdAtt.Id;
                    if (this._typeDic.TryGetValue(typeId, out oldBuilderTypeInfo))
                    {
                        if (oldBuilderTypeInfo.BuilderType == type)
                        {
                            continue;
                        }
                        else
                        {
                            throw new ArgumentException(this.GenerateErrorMessage(type, oldBuilderTypeInfo.BuilderType, typeId));
                        }
                    }
                    else
                    {
                        this._typeDic.Add(typeId, new ObjectBuilderTypeInfo(objectBuilderType.Value, type));
                        break;
                    }
                }
            }
        }

        private ObjectBuilderTypeConstructorType? GetTypeConstructorType(Type type)
        {
            ConstructorInfo[] constructorInfos = type.GetConstructors();

            if (this._requiredConstructorParameterTypes == null || this._requiredConstructorParameterTypes.Length == 0)
            {
                //仅无参数构造函数
                if (constructorInfos.Any(t => { return t.IsPublic && t.GetParameters().Length == 0; }))
                {
                    return ObjectBuilderTypeConstructorType.NoPara;
                }
                else
                {
                    ZLoger.Warn($"类型\"{type.FullName}\"不存在public修饰的无参构造函数,该类型忽略...");
                    return null; ;
                }
            }

            ObjectBuilderTypeConstructorType? objectBuilderType = null;
            ParameterInfo[] constructorParameterInfos;
            bool valid = true;
            foreach (var constructorInfo in constructorInfos)
            {
                if (!constructorInfo.IsPublic)
                {
                    //非public修饰的,忽略
                    continue;
                }

                constructorParameterInfos = constructorInfo.GetParameters();
                if (constructorParameterInfos.Length == 0)
                {
                    //存在无参构造函数
                    if (objectBuilderType.HasValue)
                    {
                        //如果有值,必然是带参构造函数,现在加入个无参的,返回ObjectBuilderTypeConstructorType.All
                        return ObjectBuilderTypeConstructorType.All;
                    }
                    else
                    {
                        objectBuilderType = ObjectBuilderTypeConstructorType.NoPara;
                    }
                    continue;
                }

                if (constructorParameterInfos.Length != this._requiredConstructorParameterTypes.Length)
                {
                    //构造函数参数个数与要求的不一致,忽略
                    continue;
                }


                valid = true;
                for (int i = 0; i < constructorParameterInfos.Length; i++)
                {
                    if (constructorParameterInfos[i].ParameterType != this._requiredConstructorParameterTypes[i])
                    {
                        valid = false;
                        break;
                    }
                }

                if (valid)
                {
                    if (objectBuilderType.HasValue)
                    {
                        //如果有值,必然是无参构造函数,现在加入个带参的,返回ObjectBuilderTypeConstructorType.All
                        return ObjectBuilderTypeConstructorType.All;
                    }
                    else
                    {
                        objectBuilderType = ObjectBuilderTypeConstructorType.Para;
                    }
                }
            }

            return objectBuilderType;
        }

        private bool CheckType(Type type, IEnumerable<Type> ignoreTypes, out string error)
        {
            error = null;
            if (type.IsAbstract)
            {
                error = $"类型{type.FullName}是抽象类,不能创建实例";
                return false;
            }

            if (ignoreTypes != null && ignoreTypes.Contains(type))
            {
                error = $"类型{type.FullName}在忽略列表中";
                return false;
            }

            if (this._baseClassType != null && !type.IsSubclassOf(this._baseClassType))
            {
                error = $"类型{type.FullName}未继承类\"{this._baseClassType.FullName}\"";
                return false;
            }

            if (this._interfaceTypes != null)
            {
                foreach (var interfaceType in this._interfaceTypes)
                {
                    if (type.GetInterface(interfaceType.FullName) == null)
                    {
                        error = $"类型{type.FullName}未实现接口\"{interfaceType.FullName}\"";
                        return false;
                    }
                }
            }

            return true;
        }

        private string GenerateErrorMessage(Type type, Type oldType, TId dataType)
        {
            string valueStr;
            if (this._errorMessageValueFormat)
            {
                Type type1 = dataType.GetType();
                if (type1.IsEnum)
                {
                    valueStr = dataType.ToString();
                }
                else
                {
                    switch (Type.GetTypeCode(type1))
                    {
                        case TypeCode.Byte:
                        case TypeCode.SByte:
                        case TypeCode.Int16:
                        case TypeCode.UInt16:
                        case TypeCode.Int32:
                        case TypeCode.UInt32:
                        case TypeCode.Int64:
                            valueStr = $"0x{Convert.ToString(Convert.ToInt64(dataType), 16)}";
                            break;
                        case TypeCode.UInt64:
                            valueStr = dataType.ToString();
                            break;
                        case TypeCode.Char:
                        case TypeCode.String:
                        default:
                            valueStr = dataType.ToString();
                            break;
                    }
                }
            }
            else
            {
                valueStr = dataType.ToString();
            }

            return $"数据类型{type.FullName}与类型{oldType.FullName}定义了重复的构建Id值\"{valueStr}\"";
        }





        private string GenerateErrorMessage(TId dataType)
        {
            string valueStr;
            if (dataType == null)
            {
                valueStr = string.Empty;
            }
            else
            {
                if (this._errorMessageValueFormat)
                {
                    Type type1 = dataType.GetType();
                    if (type1.IsEnum)
                    {
                        valueStr = dataType.ToString();
                    }
                    else
                    {
                        switch (Type.GetTypeCode(type1))
                        {
                            case TypeCode.Byte:
                            case TypeCode.SByte:
                            case TypeCode.Int16:
                            case TypeCode.UInt16:
                            case TypeCode.Int32:
                            case TypeCode.UInt32:
                            case TypeCode.Int64:
                                valueStr = $"0x{Convert.ToString(Convert.ToInt64(dataType), 16)}";
                                break;
                            case TypeCode.UInt64:
                                valueStr = dataType.ToString();
                                break;
                            case TypeCode.Char:
                            case TypeCode.String:
                            default:
                                valueStr = dataType.ToString();
                                break;
                        }
                    }
                }
                else
                {
                    valueStr = dataType.ToString();
                }
            }

            return $"不存在类型值为\"{valueStr}\"的类型";
        }

        /// <summary>
        /// 根据数据类型数据对象
        /// </summary>
        /// <param name="dataType">数据类型</param>
        /// <returns>数据对象</returns>
        public TObjectType Create(TId dataType)
        {
            TObjectType obj;
            if (this._objectBuildFactory != null && this._objectBuildFactory.TryCreate(dataType, out obj))
            {
                return obj;
            }

            ObjectBuilderTypeInfo builderTypeInfo;
            if (!this._typeDic.TryGetValue(dataType, out builderTypeInfo))
            {
                throw new ArgumentException(this.GenerateErrorMessage(dataType));
            }

            if (builderTypeInfo.ConstructorType != ObjectBuilderTypeConstructorType.NoPara &&
                builderTypeInfo.ConstructorType != ObjectBuilderTypeConstructorType.All)
            {
                throw new ArgumentException($"类型\"{builderTypeInfo.BuilderType.FullName}\"没有无参构造函数");
            }

            object objIns = Activator.CreateInstance(builderTypeInfo.BuilderType);
            return (TObjectType)objIns;
        }

        /// <summary>
        /// 根据数据类型数据对象
        /// </summary>
        /// <param name="dataType">数据类型</param>
        /// <param name="args">构造对象是.目标类型构造函数需要的参数</param>
        /// <returns>数据对象</returns>
        public TObjectType Create(TId dataType, params object[] args)
        {
            TObjectType obj;
            if (this._objectBuildFactory != null && this._objectBuildFactory.TryCreate(dataType, out obj, args))
            {
                return obj;
            }

            if (args != null &&
                this._requiredConstructorParameterTypes != null &&
                args.Length != this._requiredConstructorParameterTypes.Length)
            {
                throw new ArgumentException($"构造函数需要的参数个数不足");
            }

            ObjectBuilderTypeInfo builderTypeInfo;
            if (!this._typeDic.TryGetValue(dataType, out builderTypeInfo))
            {
                throw new ArgumentException(this.GenerateErrorMessage(dataType));
            }

            if (builderTypeInfo.ConstructorType != ObjectBuilderTypeConstructorType.Para &&
                builderTypeInfo.ConstructorType != ObjectBuilderTypeConstructorType.All)
            {
                throw new ArgumentException($"类型\"{builderTypeInfo.BuilderType.FullName}\"无匹配的带参构造函数");
            }

            object objIns = Activator.CreateInstance(builderTypeInfo.BuilderType, args);
            return (TObjectType)objIns;
        }


        /// <summary>
        /// 尝试根据数据类型数据对象
        /// </summary>
        /// <param name="dataType">数据类型</param>
        /// <param name="obj">数据对象</param>
        /// <returns>创建成功返回true,失败返回false</returns>
        public bool TryCreate(TId dataType, out TObjectType obj)
        {
            if (this._objectBuildFactory != null && this._objectBuildFactory.TryCreate(dataType, out obj))
            {
                return true;
            }

            obj = default(TObjectType);
            ObjectBuilderTypeInfo builderTypeInfo;
            if (!this._typeDic.TryGetValue(dataType, out builderTypeInfo))
            {
                return false;
            }

            if (builderTypeInfo.ConstructorType != ObjectBuilderTypeConstructorType.NoPara &&
                builderTypeInfo.ConstructorType != ObjectBuilderTypeConstructorType.All)
            {
                ZLoger.Warn($"类型\"{builderTypeInfo.BuilderType.FullName}\"没有无参构造函数");
                return false;
            }

            object objIns = Activator.CreateInstance(builderTypeInfo.BuilderType);
            obj = (TObjectType)objIns;
            return true;
        }

        /// <summary>
        /// 尝试根据数据类型数据对象
        /// </summary>
        /// <param name="dataType">数据类型</param>
        /// <param name="obj">数据对象</param>
        /// <param name="args">构造对象是.目标类型构造函数需要的参数</param>
        /// <returns>创建成功返回true,失败返回false</returns>
        public bool TryCreate(TId dataType, out TObjectType obj, params object[] args)
        {
            if (this._objectBuildFactory != null && this._objectBuildFactory.TryCreate(dataType, out obj, args))
            {
                return true;
            }

            obj = default(TObjectType);
            if (args != null &&
                this._requiredConstructorParameterTypes != null &&
                args.Length != this._requiredConstructorParameterTypes.Length)
            {
                return false;
            }

            ObjectBuilderTypeInfo builderTypeInfo;
            if (!this._typeDic.TryGetValue(dataType, out builderTypeInfo))
            {
                return false;
            }

            if (builderTypeInfo.ConstructorType != ObjectBuilderTypeConstructorType.Para &&
                builderTypeInfo.ConstructorType != ObjectBuilderTypeConstructorType.All)
            {
                ZLoger.Warn($"类型\"{builderTypeInfo.BuilderType.FullName}\"无匹配的带参构造函数");
                return false;
            }

            object objIns = Activator.CreateInstance(builderTypeInfo.BuilderType, args);
            obj = (TObjectType)objIns;
            return true;
        }






        /// <summary>
        /// 清空所有类型
        /// </summary>
        public void Clear()
        {
            this._typeDic.Clear();
        }





        /// <summary>
        /// 移除指定类型
        /// </summary>
        /// <param name="id">类型标识</param>
        public void Remove(TId id)
        {
            this._typeDic.Remove(id);
        }




        #region IReadOnlyDictionary
        /// <summary>
        /// 根据构建Id获取构造类型,参数无效将抛出异常
        /// T:System.ArgumentNullException:key is null.
        /// T:System.Collections.Generic.KeyNotFoundException:The property is retrieved and key is not found.
        /// </summary>
        /// <param name="key">构建Id</param>
        /// <returns>目标类型</returns>
        public ObjectBuilderTypeInfo this[TId key]
        {
            get
            {
                return this._typeDic[key];
            }
        }

        /// <summary>
        /// 获取构建Id集合
        /// </summary>
        public IEnumerable<TId> Keys
        {
            get { return this._typeDic.Keys; }
        }

        /// <summary>
        /// 获取构建类型集合
        /// </summary>
        public IEnumerable<ObjectBuilderTypeInfo> Values
        {
            get { return this._typeDic.Values; }
        }

        /// <summary>
        /// 获取集合元素个数
        /// </summary>
        public int Count
        {
            get { return this._typeDic.Count; }
        }

        /// <summary>
        /// 判断是否否包含某个构建Id.包含返回true,不包含返回false
        /// </summary>
        /// <param name="key">构建Id</param>
        /// <returns>包含返回true,不包含返回false</returns>
        public bool ContainsKey(TId key)
        {
            return this._typeDic.ContainsKey(key);
        }

        /// <summary>
        /// 根据构建Id获取构造类型.获取成功返回true,失败返回false
        /// </summary>
        /// <param name="key">构建Id</param>
        /// <param name="value">目标类型</param>
        /// <returns>获取成功返回true,失败返回false</returns>
        public bool TryGetValue(TId key, out ObjectBuilderTypeInfo value)
        {
            return this._typeDic.TryGetValue(key, out value);
        }

        /// <summary>
        /// 返回集合的遍历枚举器
        /// </summary>
        /// <returns>集合的遍历枚举器</returns>
        public IEnumerator<KeyValuePair<TId, ObjectBuilderTypeInfo>> GetEnumerator()
        {
            return this._typeDic.GetEnumerator();
        }

        /// <summary>
        /// 返回集合的遍历枚举器
        /// </summary>
        /// <returns>集合的遍历枚举器</returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion
    }

    /// <summary>
    /// 对象构建工厂
    /// </summary>
    /// <typeparam name="TId">数据类型值类型</typeparam>
    /// <typeparam name="TObjectType">创建实例所需要转换的基类或接口类型</typeparam>
    public interface IObjectBuildFactory<TId, TObjectType>
    {
        /// <summary>
        /// 尝试创建对象,创建成功返回true,失败返回fale
        /// </summary>
        /// <param name="id">数据类型值类型</param>
        /// <param name="type">创建成功的实例</param>
        /// <returns>创建成功返回true,失败返回fale</returns>
        bool TryCreate(TId id, out TObjectType type);

        /// <summary>
        /// 尝试创建对象,创建成功返回true,失败返回fale
        /// </summary>
        /// <param name="id">数据类型值类型</param>
        /// <param name="type">创建成功的实例</param>
        /// <param name="args">构造对象是.目标类型构造函数需要的参数</param>
        /// <returns>创建成功返回true,失败返回fale</returns>
        bool TryCreate(TId id, out TObjectType type, params object[] args);
    }

    /// <summary>
    /// 对象构建类型信息
    /// </summary>
    public class ObjectBuilderTypeInfo
    {
        /// <summary>
        /// 构建类型-构造函数类型
        /// </summary>
        public ObjectBuilderTypeConstructorType ConstructorType { get; private set; }

        /// <summary>
        /// 构造函数类型
        /// </summary>
        public Type BuilderType { get; private set; }


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="constructorType">构建类型-构造函数类型</param>
        /// <param name="builderType">构造函数类型</param>
        public ObjectBuilderTypeInfo(ObjectBuilderTypeConstructorType constructorType, Type builderType)
        {
            this.ConstructorType = constructorType;
            this.BuilderType = builderType;
        }

        /// <summary>
        /// 重写
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (this.BuilderType == null)
            {
                return "null";
            }
            else
            {
                return this.BuilderType.FullName;
            }
        }
    }

    /// <summary>
    /// 对象构建类型构造函数类型
    /// </summary>
    public enum ObjectBuilderTypeConstructorType
    {
        /// <summary>
        /// 无参数
        /// </summary>
        NoPara,

        /// <summary>
        /// 带参数
        /// </summary>
        Para,

        /// <summary>
        /// 包含无参数和带参
        /// </summary>
        All
    }
}
